package com.softlab.retailwholesalesystem.controller;

import com.softlab.retailwholesalesystem.entity.ClientEntity;
import com.softlab.retailwholesalesystem.service.IClientService;
import com.softlab.retailwholesalesystem.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/client")
@CrossOrigin
public class ClientController extends BaseController{
    @Autowired
    IClientService clientService;

    @GetMapping
    public JsonResult<List<ClientEntity>> getAllClient(){
        List<ClientEntity> clients=clientService.getAllClient();

        return new JsonResult<>(200,"查询成功",clients);
    }

    @GetMapping("/add")
    public JsonResult<Void> addClient(ClientEntity client){
        clientService.addClient(client);
        return new JsonResult<>(200,"增加成功");
    }

    @GetMapping("/search")
    public JsonResult<List<ClientEntity>> getClientByCondition(ClientEntity client){
        List<ClientEntity> clientEntities = clientService.searchClient(client);
        return new JsonResult<>(200,"查询成功",clientEntities);
    }

    @GetMapping("/delete")
    public JsonResult<Void> deleteClient(ClientEntity client){
        clientService.deleteClient(client);
        return new JsonResult<>(200,"删除成功");
    }

    @GetMapping("/update")
    public JsonResult<Void> updateClient(ClientEntity client){
        clientService.updateClient(client);
        return new JsonResult<>(200,"更新成功");
    }

    @PostMapping("/login")
    public JsonResult<ClientEntity> login(@RequestBody ClientEntity client){
        ClientEntity login = clientService.login(client);
        return new JsonResult<>(200,"登录成功",login);
    }

    @PostMapping("/register")
    public JsonResult<Void> registerClient(@RequestBody ClientEntity client){
        clientService.registerClient(client);
        return new JsonResult<>(200,"注册成功");
    }

    @PutMapping
    public JsonResult<Void> changePassword(@RequestBody ClientEntity client){
        clientService.changePassword(client);
        return new JsonResult<>(200,"修改密码成功");
    }

}
